package com.shop.api;

import com.shop.api.params.*;
import com.shop.config.annotation.TokenToUser;
import com.shop.entity.Product;
import com.shop.entity.ProductAttribute;
import com.shop.entity.User;
import com.shop.service.ProductService;
import com.shop.util.Result;
import com.shop.util.ResultGenerator;
import com.shop.util.UploadUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/product")
public class ProductApi {

    @Resource
    private ProductService productService;

    @PostMapping("/findAllProduct")
    public Result findAllProduct(@RequestParam Long storeId, @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }

        List result = productService.findAllProduct(storeId);
        if (result.size() != 0) {
            return ResultGenerator.genSuccessResult(result);
        }

        return ResultGenerator.genFailResult(50020);
    }

    @PostMapping("/createProduct")
    public Result createProduct(@RequestBody CreateProductParam createProductParam, @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }
        Product product = new Product();
        product.setProductName(createProductParam.getProductName());
        product.setCategoryId(createProductParam.getCategoryId());
        product.setStoreId(createProductParam.getStoreId());

        int result = productService.createProduct(product);
        if (result == 20000) {
            log.info("productApi,创建了一个{}产品", product.toString());
            return ResultGenerator.genSuccessResult();
        }

        return ResultGenerator.genFailResult(result);
    }

    @PostMapping("/createProductAttribute")
    public Result createProductAttribute(@RequestParam(required = false) String color,
                                         @RequestParam(required = false) String kit,
                                         @RequestParam(required = false) Double unitPrice,
                                         @RequestParam(required = false) int stock,
                                         @RequestParam(required = false) Long productId,
                                         @RequestParam(required = false) MultipartFile img,
                                         @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }
        String imgResult = null;
        if (img != null) {
            imgResult = UploadUtil.insertOrderImg(img);
            if (imgResult.length() == 5 && imgResult != "50053") {
                return ResultGenerator.genFailResult(Integer.parseInt(imgResult));
            }
        }

        ProductAttribute productAttribute = new ProductAttribute();
        productAttribute.setColor(color);
        productAttribute.setKit(kit);
        productAttribute.setUnitPrice(unitPrice);
        productAttribute.setStock(stock);
        productAttribute.setProductId(productId);
        productAttribute.setImgPath(imgResult);

        int result = productService.createProductAttribute(productAttribute, loginUser.getUserId());
        if (result == 20000) {
            log.info("productApi,创建了一个{}属性", productAttribute.toString());
            return ResultGenerator.genSuccessResult();
        }

        return ResultGenerator.genFailResult(result);
    }

    @PostMapping("/deleteProduct")
    public Result deleteProduct(@RequestParam Long productId, @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }

        boolean result = productService.deleteProduct(productId);
        if (result) {
            log.info("productApi,删除了id为{}的产品", productId);
            return ResultGenerator.genSuccessResult();
        }

        return ResultGenerator.genFailResult(50023);
    }

    @PostMapping("/deleteProductAttribute")
    public Result deleteProductAttribute(@RequestParam Long attributeId, @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }

        boolean result = productService.deleteProductAttribute(attributeId);
        if (result) {
            log.info("productApi,删除了id为{}的属性", attributeId);
            return ResultGenerator.genSuccessResult();
        }

        return ResultGenerator.genFailResult(50023);
    }

    @PostMapping("/updateProduct")
    public Result updateProduct(@RequestBody UpdateProductParam updateProductParam, @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }

        Product product = new Product();
        product.setProductId(updateProductParam.getProductId());
        product.setProductName(updateProductParam.getProductName());
        product.setCategoryId(updateProductParam.getCategoryId());
        product.setStoreId(updateProductParam.getStoreId());

        int result = productService.updateProduct(product, loginUser.getUserId());
        if (result == 20000) {
            log.info("productApi,更新了一个{}产品", product.toString());
            return ResultGenerator.genSuccessResult();
        }

        return ResultGenerator.genFailResult(result);

    }

    @PostMapping("/updateProductAttribute")
    public Result updateProductAttribute(@RequestParam(required = false) Long attributeId,
                                         @RequestParam(required = false) String color,
                                         @RequestParam(required = false) String kit,
                                         @RequestParam(required = false) Double unitPrice,
                                         @RequestParam(required = false) int stock,
                                         @RequestParam(required = false) Long productId,
                                         @RequestParam(required = false) MultipartFile img,
                                         @TokenToUser User loginUser) {
        if (loginUser == null) {
            return ResultGenerator.genFailResult(50003);//token失效
        }
        String imgResult = null;
        if (img != null) {
            imgResult = UploadUtil.insertOrderImg(img);
            if (imgResult.length() == 5 && imgResult != "50053") {
                return ResultGenerator.genFailResult(Integer.parseInt(imgResult));
            }
        }


        ProductAttribute productAttribute = new ProductAttribute();
        productAttribute.setAttributeId(attributeId);
        productAttribute.setColor(color);
        productAttribute.setKit(kit);
        productAttribute.setUnitPrice(unitPrice);
        productAttribute.setStock(stock);
        productAttribute.setProductId(productId);
        productAttribute.setImgPath(imgResult);


        int result = productService.updateProductAttribute(productAttribute);
        if (result == 20000) {
            log.info("productApi,更新了一个{}属性", productAttribute.toString());
            return ResultGenerator.genSuccessResult();
        }

        return ResultGenerator.genFailResult(result);
    }

}
